<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: groups.php Sat Jul 18 13:21:17 CST 2009
 */

include "header.php";
$category_handler = xoops_getmodulehandler("category","groups");
$groups_handler = xoops_getmodulehandler("groups","groups");

$cat_id = isset($_GET["cat_id"]) ? intval($_GET["cat_id"]) : 0 ;
$group_name = isset($_GET["group_name"]) ? trim($_GET["group_name"]) : "" ;

$start = isset($_GET["start"]) ? intval($_GET["start"]) : 0 ;

$perPage = 10 ;
$extar = "";

$criteria = new CriteriaCompo();
if ( !empty($cat_id) ) {
	$criteria->add(new Criteria("cat_id",$cat_id));
	$extar .= "cat_id=".$cat_id;
}
if ( !empty($group_name) ) {
	$criteria->add(new Criteria("group_name","%".$group_name."%","LIKE"));
	$extar .= "group_name=".$group_name;
}

if ( isset($_GET["group_status"]) && intval($_GET["group_status"]) >= 0 ) {
	$criteria->add(new Criteria("group_status",intval($_GET["group_status"])));
	$extar .= "group_status=".intval($_GET["group_status"]);
}
$criteria->setStart($start);
$criteria->setLimit($perPage);
$criteria->setOrder("DESC");
$pagenum = $groups_handler->getCount($criteria);
$xoopsTpl->assign("groupstotal",$pagenum);
if( $pagenum > $perPage){	
	$nav = new XoopsPageNav($pagenum, $perPage, $start, "start",$extar);
	$xoopsTpl->assign("pagenav", $nav->renderNav(4));
}
$group_ids = $groups_handler->getIds($criteria);

$fields = array("cat_id");
$_cat_ids = $groups_handler->getAll($criteria,$fields,false);
$cat_ids = array();
if ( $_cat_ids ) {
	foreach ( array_keys($_cat_ids) as $key ) {
		$cat_ids[] = $_cat_ids[$key]["cat_id"];
	}
}

$groups_objs = $groups_handler->getAll($criteria);
unset($criteria);

if ( !empty($cat_ids) ) {
	$criteria = new CriteriaCompo(new Criteria("cat_id",implodeArrayforSql($cat_ids),"in"));
	$cat_list = $category_handler->getAll($criteria,null,false);
	unset($criteria);
	if ( $cat_list ) {
		foreach ( array_keys($cat_list) as $key ) {
			$cat_list[$key] = $cat_list[$key]["cat_name"];
		}
	}
}

if ( !empty($group_ids) ) {
	$mem_handler = xoops_getmodulehandler("members","groups");
	$criteria = new CriteriaCompo(new Criteria("group_id",implodeArrayforSql($group_ids),"in"));
	$criteria->setGroupby("group_id");
	$mem_counts = $mem_handler->getCounts($criteria);
}

if ( $groups_objs ) {
	$groups_list = array();
	foreach ( $groups_objs as $key=>$_obj ) {
		$groups_list[$key] = $_obj->getValues();
		$groups_list[$key]["cat_name"] = $cat_list[$_obj->getVar("cat_id")];
		$groups_list[$key]["group_access_permission"] = $group_access_permission_rows[$_obj->getVar("group_access_permission")];
		$groups_list[$key]["group_post_permission"] = $group_post_permission_rows[$_obj->getVar("group_post_permission")];
		$groups_list[$key]["group_reply_permission"] = $group_reply_permission_rows[$_obj->getVar("group_reply_permission")];
		$groups_list[$key]["group_join_permission"] = $group_join_permission_rows[$_obj->getVar("group_join_permission")];
		$groups_list[$key]["group_logo"] = $_obj->groupLogo();
		$groups_list[$key]["group_status_decs"] = $group_status_rows[$_obj->getVar("group_status")];
		$groups_list[$key]["group_members"] = !empty($mem_counts[$key]) ? $mem_counts[$key] : 0 ;
		$groups_list[$key]["group_createtime"] = $_obj->createDate("Y-m-d H:i");
		
	}
	$xoopsTpl->assign("groups_list",$groups_list);
}

$cat_list = $category_handler->getCategories();
include_once(dirname(__FILE__)."../../include/form.cp.group.search.php");
$xoopsTpl->assign("cat_list",$cat_list);
$xoopsTpl->display("db:groups_cp_groups.html");
include "footer.php";
?>